/**
 * 文件：ChoiceView.java
 * 包名：com.ronri.ams.sample;   --- 正式发布时需要修正
 * 说明：单选VIEW
 * 作者：MS During
 * 日期：2017/5/11
 * 历史：
 */
package com.ronri.ams.sample;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cz.nongyetongv5.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ChoiceView extends BaseAdapter {

    private List<Map<String, Object>> mItems = new ArrayList<Map<String, Object>>();

    private Context mContext;

    public ChoiceView(Context context, List<Map<String, Object>> data) {
        super();

        mItems = data;
        mContext = context;
    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public String getItem(int position) {

        for (Map.Entry<String, Object> entry : mItems.get(position).entrySet()) {
            if(entry.getKey() == "listTitle"){
                return entry.getValue().toString();
            }
        }
        return "";
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup container) {
        int iSelect = -1;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.v_default_single_select, null);
            for (Map.Entry<String, Object> entry : mItems.get(position).entrySet()) {
                if(entry.getKey() == "selected"){
                    if (((boolean)entry.getValue()) == true){
                        iSelect =  1;
                    }
                }
            }

            convertView.setTag(mItems.get(position));
        }
        //获取条目
        CheckableFrameLayout linearLayout = (CheckableFrameLayout) convertView.findViewById(R.id.ll_contain);

        CheckableImageView imageView = ((CheckableImageView)convertView.findViewById(R.id.imageSimple));

        imageView.setTag(linearLayout);
        if (iSelect == 1){
            linearLayout.setChecked(true);
            imageView.setChecked(true);
        }else{
            linearLayout.setChecked(false);
            imageView.setChecked(false);
        }


//        imageView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                CheckableFrameLayout iTem = (CheckableFrameLayout) view.getTag();
//                if (iTem.isChecked()) {
//                    iTem.setChecked(false);
//                } else {
//                    iTem.setChecked(true);
//                }
//            }
//        });
//
//        View clickSimple = ((View)convertView.findViewById(R.id.clickSimple));
//        clickSimple.setTag(linearLayout);
//        clickSimple.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                CheckableFrameLayout iTem = (CheckableFrameLayout) view.getTag();
//                if (iTem.isChecked()) {
//                    iTem.setChecked(false);
//                } else {
//                    iTem.setChecked(true);
//                }
//            }
//        });

        ((TextView) convertView.findViewById(R.id.listTitle))
                .setText(getItem(position));

        return convertView;
    }

    public void toFinish(){
        mItems.clear();
    }
}
